package cn.javaxc.model.base;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import cn.javaxc.mvcwork.util.helper.*;
import cn.javaxc.mvcwork.util.common.*;
import cn.javaxc.model.page.VUEFormBean;

public class SQLParam extends SQL implements Map<String, Object>{
	

	
	Map map = new HashMap();
	Map defaultMap = new HashMap();
	HttpServletRequest request;
	
	public SQLParam() {
	}

	public SQLParam(HttpServletRequest request){
		this.request = request;		
	}
	public SQLParam(HttpServletRequest request,Map data){
		this.request = request;
		if (data!=null){
			map=data;	
		}
	}

	

	public Object get(Object para) {
		 return get((String)para,defaultMap.get(para));
	}
	public Object get(String para,Object _def) {
		 return RequestHelper.getRequestValue(request,map,para,_def);
	}	

	public void set(String key, Object value) {
		SQL.setParameter(map,key, value);
	}

	public Object put(String key, Object value) {
		return SQL.putParameter(map,key, value);
	}
	public void def(String key, Object value) {
		defaultMap.put(key, value);
	}
	public String where(String key, String where) {
		return SQL.where(get(key),where);
	}

	public void like(String k,Object v){
		Object o=get(k,v);
		if (!StringUtils.isNullData(o)){
			String s=o.toString();
			if (!s.startsWith("%")&&!s.endsWith("%")){
				map.put(k,"%"+s+"%");
			}
		}
	}

	public Object remove(Object key) {
		return map.remove(key);
	}

	public void clear() {
		map.clear();
	}
	public String toString() {
		return map.toString();
	}

	public boolean containsKey(Object key) {

		return map.containsKey(key)||defaultMap.containsKey(key)||RequestHelper.getRequestValue(request,(String)key)!=null;
	}

	public boolean containsValue(Object value) {

		return map.containsValue(value);
	}

	public Set entrySet() {

		return map.entrySet();
	}

	public boolean isEmpty() {

		return map.isEmpty();
	}

	public Set keySet() {

		return map.keySet();
	}


	public void putAll(Map m) {
		if (m instanceof SQLParam){
			this.request = ((SQLParam)m).getRequest();
		}
		map.putAll(m);
	}

	public int size() {

		return map.size();
	}

	public Collection values() {

		return map.values();
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public VUEFormBean getFormBean() {
		VUEFormBean bean=(VUEFormBean)request.getAttribute("elementsmap");
		if (bean==null){
			throw new RuntimeException("getFormBean");
		}
		return bean;
	}
}
